<template>
  <div>
    <el-card style="margin-bottom: 20px">
      <CategorySelect
        @getCategoryList="getCategoryList"
        :show="scene != 0"
      ></CategorySelect>
    </el-card>

    <el-card>
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="100" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <MessageButton
                title="添加spu"
                type="success"
                icon="el-icon-plus"
                size="mini"
                @click="addSku(row)"
              ></MessageButton>
              <MessageButton
                title="修改spu"
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="updateSpu(row)"
              ></MessageButton>
              <MessageButton
                title="查看当前spu"
                type="info"
                icon="el-icon-info"
                size="mini"
                @click="handler(row)"
              ></MessageButton>
              <MessageButton
                title="删除"
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteSpu(row)"
              ></MessageButton>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          @current-change="getDataList"
          @size-change="handleSizeChange"
          style="margin-top: 20px; text-align: center"
          background
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
        >
        </el-pagination>
      </div>
      <SpuForm
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2"
        ref="sku"
        @changeScenes="changeScenes"
      ></SkuForm>
    </el-card>
    <el-dialog
      :title="`${spu.spuName}`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称"></el-table-column>
        <el-table-column prop="price" label="价格"></el-table-column>
        <el-table-column prop="weight" label="重量"></el-table-column>
        <el-table-column label="默认图片">
          <template slot-scope="{ row }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  name: "Spu",
  components: { SkuForm, SpuForm },
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      total: 0,
      limit: 5,
      page: 1,

      records: [],

      scene: 0, //0--展示records列表，1---添加或修改spu   2-添加sku
      dialogTableVisible: false,
      spu: {},
      skuList: [],
      loading: true,
    };
  },
  methods: {
    //接受返回过来的三级分类的id
    getCategoryList({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId;
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
      } else if (level == 3) {
        this.category3Id = categoryId;
        //发送请求
        this.getDataList();
      }
    },

    //发送请求获取table数据展示
    async getDataList(pager = 1) {
      this.page = pager;
      const { page, limit, category3Id } = this;
      try {
        let result = await this.$API.spu.reqSpuList(page, limit, category3Id);
        if (result.code == 200) {
          console.log(result);
          this.total = result.data.total;
          this.records = result.data.records;
        }
      } catch (error) {}
    },

    handleSizeChange(limit) {
      this.limit = limit;
      this.getDataList();
    },

    //显示底下展示的内容
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.category3Id);
    },

    //修改某一个spu
    updateSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row);
    },

    //spuform自定义事件
    changeScene({ scene, flag }) {
      this.scene = scene;
      if (flag == "添加") {
        this.getDataList();
      } else if (flag == "修改") {
        this.getDataList(this.page);
      }
    },

    //添加sku
    addSku(row) {
      this.scene = 2;
      //获取sku
      this.$refs.sku.getData(this.category1Id, this.category2Id, row);
    },

    changeScenes(scene) {
      this.scene = scene;
    },

    async handler(spu) {
      this.dialogTableVisible = true;
      this.spu = spu;
      let result = await this.$API.sku.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data;
        this.loading = false;
      }
    },

    close(done) {
      this.loading = true;
      this.skuList = [];
      done();
    },

    async deleteSpu(row) {
      console.log(row);
      let result = await this.$API.sku.reqDelete(row.id);

      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "删除成功",
        });
        this.getDataList(this.pager);
      }
    },
  },
};
</script>

<style>
</style>